<?php

declare(strict_types=1);

namespace App\Models;

use App\Core\Database;
use App\Helpers\Date;
use flight\ActiveRecord;

/**
 * Clase base para todos los modelos.
 */
abstract class BaseModel extends ActiveRecord
{
    protected string $primaryKey = 'id';

    /**
     * Método abstracto dependiente que obtiene la tabla del modelo.
     */
    abstract public function getTable(): string;

    /**
     * Constructor heredado de la clase.
     */
    public function __construct()
    {
        parent::__construct(Database::getConnection(), $this->getTable());
    }

    /**
     * Establece campos antes de insertarlos.
     */
    protected function beforeInsert(self $self): void
    {
        $self->id = Database::generateUuid();
        $self->created_at = Date::getCurrentDatetime();
        $self->updated_at = Date::getCurrentDatetime();
    }

    /**
     * Establece campos antes de modificarlos.
     */
    protected function beforeUpdate(self $self): void
    {
        $self->updated_at = Date::getCurrentDateTime();
    }
}
